package com.xiang.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class test07 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        Class c1 = Class.forName("com.xiang.reflection.User");


        //获得类的名字
        System.out.println(c1.getName());//获得包名+类名
        System.out.println(c1.getSimpleName());//获得类名

        //获得类的属性
        Field[] fields = c1.getFields(); //只能找到public属性

        Field[] declaredFields = c1.getDeclaredFields();//能找到所有属性
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }

        //获得类的方法,这个是获得所有的方法
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("======分割线======");
        //获得本类的所有方法
        Method[] declaredMethods = c1.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }


        System.out.println("======分割线======");
        //获得指定方法
        Method getName = c1.getMethod("getName", null);
        //需要什么丢什么值，方法名重载，需要塞入参数，这样就能判定是哪个方法，避免出现重名方法冲突
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        System.out.println("======分割线======");
        //获得指定的构造器
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }

        System.out.println("======分割线======");
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }

        System.out.println("======分割线======");
        //获得指定的构造器（丢入参数获取指定重载的）
        Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
        System.out.println(declaredConstructor);
    }
}
